package com.acwing.lc.part1;

/**
 * @author rkc
 * @date 2022/9/17 10:53
 */
public class LC67二进制求和 {

    public String addBinary(String a, String b) {
        a = reverse(a);
        b = reverse(b);
        StringBuilder ans = new StringBuilder();
        for (int i = 0, t = 0; i < a.length() || i < b.length() || t > 0; i++) {
            if (i < a.length()) t += a.charAt(i) - '0';
            if (i < b.length()) t += b.charAt(i) - '0';
            ans.append(t % 2);
            t /= 2;
        }
        return ans.reverse().toString();
    }

    private String reverse(String s) {
        char[] arr = s.toCharArray();
        for (int l = 0, r = arr.length - 1; l < r; l++, r--) {
            char t = arr[l];
            arr[l] = arr[r];
            arr[r] = t;
        }
        return new String(arr);
    }
}
